#include "lcd.h"

LCD::LCD()
    :x(x=0)
{
    this->fb = open("/dev/fb0",O_RDWR);
    if(fb == -1)
    {
        printf("lcd打开失败:%s\n",strerror(errno));
        exit(0);
    }
    fb_var_screeninfo vinfo;
    fb_fix_screeninfo finfo;
    ioctl(fb,FBIOGET_VSCREENINFO,&vinfo);
    ioctl(fb,FBIOGET_FSCREENINFO,&finfo);

    wide   = vinfo.xres;
    high   = vinfo.yres;
    bpp    = vinfo.bits_per_pixel;
    tosize = vinfo.xres*vinfo.yres*vinfo.bits_per_pixel/8;

    //映射2倍大小方便更新可见区
    men    = (char *)mmap(NULL,tosize*2,PROT_READ|PROT_WRITE,
    MAP_SHARED,fb,0);
    if(men == MAP_FAILED)
    {
        printf("内存映射失败:%s\n",strerror(errno));
        exit(0);
    }
}
LCD::LCD(int x)
{
    this->x = x;
    this->fb = open("/dev/fb0",O_RDWR);
    if(fb == -1)
    {
        printf("lcd打开失败:%s\n",strerror(errno));
        exit(0);
    }
    fb_var_screeninfo vinfo;
    fb_fix_screeninfo finfo;
    ioctl(fb,FBIOGET_VSCREENINFO,&vinfo);
    ioctl(fb,FBIOGET_FSCREENINFO,&finfo);

    wide   = vinfo.xres;
    high   = vinfo.yres;
    bpp    = vinfo.bits_per_pixel;
    tosize = vinfo.xres*vinfo.yres*vinfo.bits_per_pixel/8;

    //映射2倍大小方便更新可见区
    men    = (char *)mmap(NULL,tosize*2,PROT_READ|PROT_WRITE,
    MAP_SHARED,fb,0);
    if(men == MAP_FAILED)
    {
        printf("内存映射失败:%s\n",strerror(errno));
        exit(0);
    }

    frags();
}

LCD::~LCD()
{
    close(fb);
    munmap(men,tosize*2);
    cout << "lcd的析构函数成功调用" << endl;
}
/*给lcd二维数组分配目标*/
/*已在2号构造函数中调用*/
void LCD::frags()
{
    int nwidth = wide/x;
    int nheight = high/x;
    int p = nwidth * bpp/8;
    int q = wide * bpp/8;
    pm.resize(x);

    // 设置二维数组的大小为x*x  
    pm.resize(x);  
    for (int i = 0; i < x; ++i) {  
        pm[i].resize(x);  
        for (int j = 0; j < x; ++j) {  
            pm[i][j] = NULL; 
        }  
    } 

    for(int n=0;n<x;n++)
    {
        for(int m=0;m<x;m++)
        {
            pm[n][m] = men + p*m + q*nheight*n;
        }
    }
}